package com.itheima.springboot_21_j2cache.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.springboot_21_j2cache.domain.Book;
import com.itheima.springboot_21_j2cache.service.IBookService;
import com.itheima.springboot_21_j2cache.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController // == @Controller+@ResponseBody //表示这个类的方法都有返回值
@RequestMapping("/books") //访问/books就能访问到这个类
public class BookController2 {
    private static final Logger log= LoggerFactory.getLogger(BookController2.class);
    //创建记录日志的对象

    @Autowired
    private IBookService bookService;
    @GetMapping
    public R getAll(){ //
        return new R(true,bookService.list());
    }
    @PostMapping //post发送方式,参数类型是json,raw  http://127.0.0.1:8081/books
    public R save(@RequestBody Book book){ //RequestBody 请求体参数
        //R  r=new R();
        //boolean flag=bookService.save(book);
        //r.setFlag(flag);
//        new R(bookService.save(book));
        Boolean flag=bookService.save(book);
        return new R(flag,flag?"成功":"失败");//这个后面的文字描述,是给前端的状态现实用的.从此前端气泡里的文字由后端掌控
    }

    //{
    //    "type": "纸质版",
    //    "name": "水浒传",
    //    "description": "宋江2"
    //}

    @PutMapping
    public R update(@RequestBody Book book){ //RequestBody 请求体参数
        return new R(bookService.updateById(book));
    }
    @DeleteMapping("{id}")//路径参数 http://127.0.0.1/books/2
    public R delete(@PathVariable Integer id){ // PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return new R(bookService.removeById(id));
    }
    @GetMapping("{id}")//路径参数 http://127.0.0.1/books/3
    public R getById(@PathVariable Integer id){// PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return new R(true,bookService.getById(id));
    }
    @GetMapping("{id}/{id2}/{id3}")//路径参数 http://127.0.0.1/books/3/2/1
    public R testSetStringCache(@PathVariable Integer id,@PathVariable Integer id2,@PathVariable Integer id3){// PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return new R(true,bookService.testSetStringCache(id,id2,id3));
    }

    @GetMapping("{id}/{id2}/{id3}/{id4}")//路径参数 http://127.0.0.1/books/3/2/1/0
    public R testJetCacheInteger(@PathVariable Integer id,@PathVariable Integer id2,@PathVariable Integer id3,@PathVariable Integer id4){// PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return new R(true,bookService.testJetCacheInteger(id,id2,id3,id4));
    }

    @GetMapping("{id}/{id2}/{id3}/{id4}/{id5}")//路径参数 http://127.0.0.1:8080/books/4/2/1/0/-1
    public R testJetCacheInteger2(@PathVariable Integer id,@PathVariable Integer id2,@PathVariable Integer id3,@PathVariable Integer id4,@PathVariable Integer id5){// PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return new R(true,bookService.testJetCacheInteger2(id,id2,id3,id4,id5));
    }

    @PutMapping("{id}")//路径参数 http://127.0.0.1:8080/books/4 //后面的参数并没有用,仅仅是为了区分路由
    public R testJetCacheInteger2Update(@RequestBody Book book){// PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return new R(true,bookService.testJetCacheInteger2Update(book));
    }

    @DeleteMapping("{id}/{id2}")//路径参数 http://127.0.0.1:8080/books/4/3 //后面的参数并没有用,仅仅是为了区分路由
    public R testJetCacheInteger2Delete(@PathVariable Integer id,@PathVariable Integer id2){ // PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return new R(bookService.testJetCacheInteger2Delete(id)>0);
    }

    //@GetMapping("{currentPage}/{pageSize}") //http://127.0.0.1:8081/books/3/2
    //public R getPage(@PathVariable int currentPage, @PathVariable int pageSize){
    //    //System.out.println(1/0);
    //    //return new R(true,bookService.getPage(currentPage,pageSize));
    //    IPage<Book> page=bookService.getPage(currentPage,pageSize);
    //    //如果当前页码大于最大页码,那么重新执行查询,使用最大页码作为当前页码
    //    if(currentPage>page.getPages()){
    //        page=bookService.getPage((int)page.getPages(),pageSize);
    //    }
    //    return new R(true,page);
    //}
    @GetMapping("{currentPage}/{pageSize}") //http://127.0.0.1:8081/books/3/2
    public R getPage(@PathVariable int currentPage, @PathVariable int pageSize,String name){//最后这个name参数通过params参数传过来
        log.info("info...7777");//记录日志,他们在日志显示的行头不同INFO
        log.error("info...");//记录日志,他们在日志显示的行头不同ERROR
        log.debug("info...");//记录日志,他们在日志显示的行头不同WARN
        log.warn("info...");//记录日志,他们在日志显示的行头不同

        //System.out.println(name+"###############################");
        //System.out.println(1/0);
        //return new R(true,bookService.getPage(currentPage,pageSize));
        IPage<Book> page=bookService.getPage(currentPage,pageSize,name);
        //如果当前页码大于最大页码,那么重新执行查询,使用最大页码作为当前页码
        if(currentPage>page.getPages()){
            page=bookService.getPage((int)page.getPages(),pageSize,name);
        }
        return new R(true,page);
    }
}
